/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util.ejb;

import entities.tools.Form;
import entities.users.FormOwner;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Bean used for performing the operations on the Form Owners entities
 *
 * @author Marek
 */
@Stateless
public class FormOwnerBean implements FormOwnerBeanInterface {

    @PersistenceContext(name = "test", unitName = "JEE-study-warPU")
    private EntityManager em;

    /**
     * Method creates new FormOwner instance
     *
     * @return new FormOwner instance
     */
    @Override
    public FormOwner createFormOwner() {
        FormOwner formOwner = new FormOwner();
        em.persist(formOwner);
        return formOwner;
    }

    /**
     * Method deletes specified by detachedFormOnwer entity
     *
     * @param detachedFormOwner
     * @return true if operation succeeded, false - if not
     */
    @Override
    public boolean deleteFormOwner(FormOwner detachedFormOwner) {
        FormOwner attachedFormOwner = em.find(FormOwner.class, detachedFormOwner.getId());
        if (null == attachedFormOwner) {
            System.out.println("FormOwnerBean:deleteFormOwner():FormOwner, you have specified, doesn't exist in the database!");
            return false;
        }
        Long size = (Long) em.createQuery("select count(form) from FormOwner formOwner,Form form where formOwner=:formOwner and formOwner=form.formOwner").setParameter("formOwner", attachedFormOwner).getSingleResult();
        if (size > 0) {
            System.out.println("FormOwnerBean:deleteFormOwner():FormOwner cannot be deleted, becouse he is still owning some forms. Firstly delete his forms!");
            return false;
        }
        em.remove(attachedFormOwner);
        return true;
    }

    //updates the existing form owner
    /**
     * Method updates in the database, specified by detachedFormOwner entity
     *
     * @param detachedFormOwner
     * @return true if operation succeeded, false - if not
     */
    @Override
    public boolean updateFormOwner(FormOwner detachedFormOwner) {
        FormOwner attachedFormOwner = em.find(FormOwner.class, detachedFormOwner.getId());
        if (null == attachedFormOwner) {
            System.out.println("FormOwnerBean:updateFormOwner():FormOwner, you have specified, doesn't exist in the database!");
            return false;
        }
        em.merge(detachedFormOwner);
        return true;
    }

    /**
     * Method find in the detabase entity specified by id parameter, and returns
     * this entity
     *
     * @param id
     * @return founded FormOwner
     */
    @Override
    public FormOwner getFormOwner(Long id) {
        FormOwner attachedFormOwner = em.find(FormOwner.class, id);
        if (null == attachedFormOwner) {
            System.out.println("FormOwnerBean:getFormOwner():FormOwner, you have specified, doesn't exist in the database!");
            return null;
        }
        return attachedFormOwner;
    }

    /**
     * Method returns all FormOwner entities from the database
     *
     * @return all FormOwner from the database
     */
    @Override
    public List<FormOwner> getFormOwners() {
        return em.createQuery("select formOwner from FormOwner formOwner", FormOwner.class).getResultList();
    }

    /**
     * Method returns all forms owned by entity set in detachedFormOwner param.
     *
     * @param detachedFormOwner
     * @return List of all filled forms
     */
    @Override
    public List<Form> getFilledForms(FormOwner detachedFormOwner) {
        FormOwner attachedFormOwner = em.find(FormOwner.class, detachedFormOwner.getId());
        if (null == attachedFormOwner) {
            System.out.println("FormOwnerBean:getFilledForms():FormOwner, you have specified, doesn't exist in the database!");
            return null;
        }
        return attachedFormOwner.getForms();
    }

    /**
     * (Dangerous) Method is bypasses security policy. Normally FormOwner cannot
     * be deleted, when he had filled some forms before. Method is performing
     * delete operation on all instances of FilledForm entity related to the
     * Form Owner, specified by pram, and than it deletes also that FormOwner
     * entity, specified by param.
     *
     * @param detachedFormOwner
     * @return true - if operation succeeded false - otherwise
     */
    //TODO: Method need to be tested, if it works correctly
    @Override
    public boolean forcedDeleteFormOwner(FormOwner detachedFormOwner) {
        /*
        //TODO: create that method in the future
        */
        return true;
    }
    
    
    //TODO: register the existing form, as a property of the existing form owner
    //TODO: remove the existing form, from the existing form owner, property list
    //TODO: register the existing form signer, as a connected to the form owner
    //TODO: remove the existing form signer, from the form owner connections with form signers list
}
